<template>
  <div class="login">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>

    <!-- 登录表单 -->
    <van-form ref="loginFrom" @submit="onSubmit">
      <!-- 手机号 -->
      <van-field
        v-model="user.mobile"
        name="mobile"
        :rules="rulesForm.mobile"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="iconfont iconshouji"></i>
      </van-field>

      <!-- 验证码 -->
      <van-field
        v-model="user.code"
        name="code"
        :rules="rulesForm.code"
        placeholder="请输入验证码"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="iconfont iconyanzhengma"></i>
        <template #button>
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 60"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            native-type="button"
            round
            size="small"
            type="default"
            class="send-btn"
            @click="onSendVc"
            >发送验证码</van-button
          >
        </template>
      </van-field>

      <!-- 提交按钮 -->
      <div class="login-btn-wap">
        <van-button block type="info" native-type="submit" class="login-btn"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login_api, sendVc } from "@/api";

export default {
  name: "login",
  data() {
    return {
      // 数据绑定
      user: {
        // mobile: "13911111111",
        mobile: "15549057495",
        code: "246810"
      },
      // 验证规则
      rulesForm: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空"
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "验证码格式错误"
          }
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空"
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误"
          }
        ]
      },
      // 是否展示倒计时
      isCountDownShow: false
    };
  },
  methods: {
    // 表单验证
    async onSubmit() {
      // 1.获取表单数据
      const user = this.user;
      // 2.表单验证
      this.$toast.loading({
        message: "登录中...",
        forbidClick: true,
        duration: 0
      });
      // 3.提交表单请求登录
      try {
        const { data } = await login_api(this.user);
        console.log("登录成功",data,data.data)
        this.$store.commit("setUser", data.data);
        this.$toast.success("登录成功");
        this.$router.back();
      } catch (err) {
        if (err.response.status === 400) {
          console.log("手机号或验证码错误");
          this.$toast.fail("手机号或验证码错误");
        } else {
          console.log("登录失败,请稍后重试", err);
          this.$toast.fail("登录失败,请稍后重试");
        }
        // console.log("err",err)
      }
      // 4.根据请求响应结果处理后续操作
    },
    // 验证码验证
    async onSendVc() {
      // console.log('onSendVc')
      // 1.校验手机号
      try {
        await this.$refs.loginFrom.validate("mobile");
      } catch (err) {
        return console.log("验证失败", err);
      }
      // 2.验证通过，显示倒计时
      this.isCountDownShow = true;
      // 3.请求发送验证码
      try {
        await sendVc(this.user.mobile);
        this.$toast("发送成功");
      } catch (err) {
        // 发送失败，关闭倒计时
        this.isCountDownShow = false;
        if (err.response.status === 429) {
          this.$toast("发送频繁,请稍后重试");
        } else {
          this.$toast("发送失败,请稍后重试");
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
/deep/ .van-button__text {
  width: 200px;
}
.iconfont {
  font-size: 37px;
}
.send-btn {
  background-color: #ededed;
  width: 152px;
  height: 46px;
  line-height: 46px;
  font-size: 22px;
  color: #666;
}
</style>
